<?php
/**
 * 流程：
 * 1、https://api.weibo.com/oauth2/authorize?client_id=2799866073&response_type=code&redirect_uri=http://127.0.0.1/weibo.com/demo2.0/callback.php
 */

$data = array (
  'client_id' => '2799866073',
  'client_secret' => '342c40ea0bdec7319b07ef67f98d1494',
  'grant_type' => 'authorization_code',
  'redirect_uri' => 'http://127.0.0.1/weibo.com/demo2.0/callback.php',
  'code' => '4e8875d1b09113448ccbb3625ee3c995',
);
$tmp = curl("https://api.weibo.com/oauth2/access_token", $data);
var_dump($tmp);

function curl($url, $postFields = null)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_FAILONERROR, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
	
	///
	if (is_array($postFields) && 0 < count($postFields)) {
		$postBodyString = "";
		$postMultipart = false;
		foreach ($postFields as $k => $v) {
			if("@" != substr($v, 0, 1)) 
				// 判断是不是文件上传
				$postBodyString .= "$k=" . urlencode($v) . "&"; 
			else 
				// 文件上传用multipart/form-data，否则用www-form-urlencoded
				$postMultipart = true;
		}
		unset($k, $v);
		
		///
		curl_setopt($ch, CURLOPT_POST, true);
		if ($postMultipart) 
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
		else 
			curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
	}
	
	///
	try {
		$reponse = curl_exec($ch);
		if (curl_errno($ch)) 
			throw new Exception(curl_error($ch), 0);
		else {
			$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
			if (200 !== $httpStatusCode) 
				throw new Exception($reponse, $httpStatusCode);
		}
	} catch (Exception $e) {
		///
		return null;
	}
	
	///
	curl_close($ch);
	return $reponse;
}
